﻿@model  IList<FB.Providers.EmployeeInfo.Company>
@{
    ViewBag.Title = "选择人";
    Layout = "~/Views/Shared/_FormLayout.cshtml";
}
@section HeadIncluder 
{
 
    <script src=@Url.Content("/Scripts/jquery.form.js") type="text/javascript"></script>
    <script type="text/javascript" scr="./js/jquery.js"></script>
     <link href='@Url.Content("~/Content/public.css")' rel="stylesheet" />
    <link href='@Url.Content("~/Content/con_style.css")' rel="stylesheet" />
    <style type="text/css">
        body {
             height:400px;
        }
    </style>
}
<div class="xz_dhk">
    <form id="form1" name="form1" method="post" action="">
        <div class="xz_table">
            <table class="xz_t">
                <tr>
                 <td class="radio1"><input name="SearchType" value="1" type="radio" checked="checked" id="td0" class="on" onclick="selectMenu(this, 'tab0')" />用户 </td>
                 <td class="radio1"><input name="SearchType" value="2" type="radio" id="td1" class="on" onclick="selectMenu(this, 'tab1')" />角色</td>
                 <td class="radio1"><input name="SearchType" value="3" type="radio" id="td2" class="on" onclick="selectMenu(this, 'tab2')" />部门岗位</td>
                 <td class="radio1"><input name="SearchType" value="4" type="radio" />分组</td>
                 <td width="100"><input id="cbLeaveEmployee" type="checkbox" />显示离职人员</td>
                </tr>
            </table>
            <table class="xz_t">
                <tr>
                    <td width="60">查询内容</td>
                    <td width="170">
                        <input style="width: 150px;" id="txtQuery" class="in_c2" type="text" /></td>
                    <td width="40">
                        <button id="btnQuery" class="fj btn_c1 btn_s" type="button" onclick="Selete()">查找</button></td>
                    <td width="40">
                        <button id="reset" class="fj btn_c1 btn_s" onclick="reset()">清除</button></td>
                </tr>
               
            </table>
        </div>
        <div class="xz_table" style="height:400px">
            <table width="100%" class="xz_t" style="height:400px">
                <tr>
                    <td style="width: 300px;">
                        <table width="100%" class="xz_t"  style="display: none;"  id="tab0">
                            <tr>
                                <td style="height: 350px;">
                                    <div class="xz_con mar_r">
                                        <p class="xz_tit">用户</p>
                                        <div id="divTree" style="height: 370px;"></div>
                                    </div>
                                </td>
                            </tr>
                        </table>
                        <table width="100%" class="xz_t" style="display: none;" id="tab1">
                            <tr>
                                <td>
                                    <div class="xz_con mar_r">
                                        <p class="xz_tit">角色</p>
                                        <div id="divTreeRole" style="height: 370px;"></div>
                                    </div>
                                </td>
                            </tr>
                        </table>
                        <table width="100%" class="xz_t" style="display: none;" id="tab2">
                            <tr>
                                <td>
                                    <div class="xz_con mar_r">
                                        <p class="xz_tit">部门岗位</p>
                                        <div id="divTreeDeparment" style="height: 370px;"></div>
                                    </div>
                                </td>
                            </tr>
                        </table>
                        <table width="100%" class="xz_t" style="display: none;" id="tab3">
                            <tr>
                                <td>
                                    <div class="xz_con mar_r">
                                        <p class="xz_tit">查找到的用户</p>
                                        <div id="divTreeSelete" style="height: 370px;"></div>
                                    </div>
                                </td>
                            </tr>
                        </table>

                    </td>
                    <td>
                        <div class="xz_btn">
                            <button type="button" id="btnSelect" onclick="deleteAll()" class="fj btn_c1 btn_s mar20" style="display: block;"> 全部删除 </button>
                        </div>
                    </td>
                     <td>
                             <table width="100%" class="xz_t" >
                                 <tr>
                                     <td>
                                         <div class="xz_con" >
                                             <p class="xz_tit">已经选择的</p>
                                             <ul id="selectedEmployees" style="height: 370px;" class="validate[required]">
                                                 @if (ViewBag.EmployeeList != null)
                                                 {
                                                     IList<FB.Providers.EmployeeInfo.Employee> employeeList = ViewBag.EmployeeList as IList<FB.Providers.EmployeeInfo.Employee>;
                                                     foreach (var item in employeeList)
                                                     {
                                                     <li><a onclick='DeleteEmplyee(this)'  UserID='@item.UserID'  data='@item.EMPCode' href='javascript:void(0)'>@item.ChineseName</a></li>
                                                }
                                            }
                                             </ul>
                                         </div>
                                     </td>
                                 </tr>
                             </table>
                    </td>
                </tr>
                <tr>
                    
                    <td></td>
                    <td></td>
                    <td><div class="cxl">
                        <p style="text-align: right; margin-top: 10px;">
                            <button type="button" onclick="cancel();" class="fj btn_c1 btn_s">取消</button>
                            <button id="subbtn" type="button" class="ui_state_highlight fj btn_c1 btn_s">确定</button>
                        </p>
                         </div>
                    </td>
                </tr>
            </table>            
        </div>
    </form>
</div>
<script type="text/javascript">
   
    $(document).ready(function () {
        selectMenu(document.getElementById('td0'), 'tab0');
    })
    function selectMenu(oThis, tabID) {
        $(".tbShow").hide();
        $(".tbShow").removeClass("tbShow");
        $("#"+tabID).show();
        $("#"+tabID).addClass("tbShow");

    }
    function deleteAll() {
        var lock = '@Request["Lock"]';
        if(lock==null||lock=='')
        {
            $("#selectedEmployees li").remove();
        } 
        
    }
    function reset() {
        $("#txtQuery").val("");
    }
    function Selete() {
        var treeview = $("#divTreeSelete").data("kendoTreeView");
        if (treeview) {
            treeview.destroy();
        }
        GetEmployeesBySelete();
        selectMenu(this, 'tab3');
    }
    function GetEmployeesByEmployee() {
        var serviceRoot = '@Url.Action("EmployeeSelect","Employee")';
        homogeneous = new kendo.data.HierarchicalDataSource({
            transport: {
                read: {
                    url: serviceRoot ,
                    dataType: "json"
                }
            },
            schema: {
                model: {
                    id: "ObjectID",
                    hasChildren: "HasChildren"
                }
            }
        });

        $("#divTree").kendoTreeView({
            dataSource: homogeneous,
            dataTextField: "Name",
            select: function (e) { 
                var obj = this.dataItem(e.node);
                var name = obj.Name;
                var userID = obj.UserID;
                var item = JSON.parse(obj.ObjectID);
                if (item.Value == "Employee") {
                    SelectEmployee(item.Key, name, userID);
                }

            }
        });
    }
    function GetEmployeesByRole() {
        var serviceRoot = '@Url.Action("RoleSelect","Employee")';
         homogeneous = new kendo.data.HierarchicalDataSource({
             transport: {
                 read: {
                     url: serviceRoot,
                     dataType: "json"
                 }
             },
             schema: {
                 model: {
                     id: "ObjectID",
                     hasChildren: "HasChildren"
                 }
             }
         });

         $("#divTreeRole").kendoTreeView({
             dataSource: homogeneous,
             dataTextField: "Name",
             select: function (e) {
                 var obj = this.dataItem(e.node);
                 var name = obj.Name;
                 var userID = obj.UserID;
                 var item = JSON.parse(obj.ObjectID);
                 if (item.Value == "Employee") {
                     SelectEmployee(item.Key, name, userID);
                 }

             }
         });
    }
    function GetEmployeesByDeparment() {
        var serviceRoot = '@Url.Action("DeparmentSelect","Employee")';
         homogeneous = new kendo.data.HierarchicalDataSource({
             transport: {
                 read: {
                     url: serviceRoot,
                     dataType: "json"
                 }
             },
             schema: {
                 model: {
                     id: "ObjectID",
                     hasChildren: "HasChildren"
                 }
             }
         });

         $("#divTreeDeparment").kendoTreeView({
             dataSource: homogeneous,
             dataTextField: "Name",
             select: function (e) {
                 var obj = this.dataItem(e.node);
                 var name = obj.Name;
                 var userID = obj.UserID;
                 var item = JSON.parse(obj.ObjectID);
                 if (item.Value == "Employee") {
                     SelectEmployee(item.Key, name, userID);
                 }

             }
         });
     }
    function GetEmployeesBySelete() {
        var SeleteKey = $("#txtQuery").val();
        var isLeave = 0;
        if ($("#cbLeaveEmployee").attr("checked") == "checked") {
            isLeave = 1;
        }
        var serviceRoot = '@Url.Action("SelectEmployees","Employee")' + "?Key=" + SeleteKey + "&isLeave="+isLeave;
       homogeneous = new kendo.data.HierarchicalDataSource({
           transport: {
               read: {
                   url: serviceRoot,
                   dataType: "json"
               }
           },
           schema: {
               model: {
                   id: "ObjectID",
                   hasChildren: "HasChildren"
               }
           }
       });

       $("#divTreeSelete").kendoTreeView({
           dataSource: homogeneous,
           dataTextField: "Name",
           select: function (e) {
               var obj = this.dataItem(e.node);
               var name = obj.Name;
               var userID = obj.UserID;
               var item = JSON.parse(obj.ObjectID);
               if (item.Value == "Employee") {
                   SelectEmployee(item.Key, name, userID);
               }

           }
       });
   }
    function GetEmployeesByMyTeam() {
        var serviceRoot = '@Url.Action("MyTeamSelect","Employee")';
        homogeneous = new kendo.data.HierarchicalDataSource({
            transport: {
                read: {
                    url: serviceRoot,
                    dataType: "json"
                }
            },
            schema: {
                model: {
                    id: "ObjectID",
                    hasChildren: "HasChildren"
                }
            }
        });

        $("#divTreeMyTeam").kendoTreeView({
            dataSource: homogeneous,
            dataTextField: "Name",
            select: function (e) {
                var obj = this.dataItem(e.node);
                var name = obj.Name;
                var userID = obj.UserID;
                var item = JSON.parse(obj.ObjectID);
                if (item.Value == "Employee") {
                    SelectEmployee(item.Key, name, userID);
                }

            }
        });
    }
    function cancel() {
        if (window.opener != undefined && window.opener != null) {
            window.close();
        } else {
            window.top.oDG.close();
        }
    }
    $(function () {
        GetEmployeesByEmployee();
        GetEmployeesByRole();
        GetEmployeesByDeparment();
        GetEmployeesByMyTeam();
        $("#subbtn").click(function () { 
            if ($("#selectedEmployees a").length == 0) {
                alert("你还没有选择对应的人！");
                return;
            }
            var arr = new Array();
            $.each($("#selectedEmployees a"), function (i, item) {
                var obj = new Object();
                obj.EMPCode = $(item).attr("data");
                obj.UserID = $(item).attr("UserID");
                obj.Name = $(item).html();
                arr.push(obj);
            });
            var windowcallback = '@Request["windowcallback"]';
            if (windowcallback != "" && window.opener != undefined && window.opener != null) {
                switch(windowcallback)
                {
                    case "AddEmployeeCallBack": 
                        window.opener.AddEmployeeCallBack(arr);
                        break;
                    case "RefreshValue":
                        window.opener.RefreshValue(arr);
                        break;
                }
                window.close();
            } else {
                window.top.refresh(arr);
                window.top.oDG.close();
            }
        });

    });
    function SelectEmployee(EmplyeeCode, EmployeeName,UserID) {
        var str = " <li ><a onclick='DeleteEmplyee(this)'  UserID='" + UserID + "' data='" + EmplyeeCode + "' href='javascript:void(0)'>" + EmployeeName + "</a></li>";
        var item = $("#selectedEmployees li a");
        for (var i = 0; i < item.length; i++) {
            if ($(item[i]).attr("UserID") == UserID) {
                return false;
            }
        }
        var lock = '@Request["Lock"]';
        if (lock == null || lock == '') {
            $(str).appendTo("#selectedEmployees");
        }
    }
    function DeleteEmplyee(tag) {
        var lock = '@Request["Lock"]';
        if (lock == null || lock == '') { 
            $(tag).parent().remove();
        }
    }
</script>

